/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysDeptApiController.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年2月1日
 */
package com.sirdc.modules.sys.web.api;

import javax.servlet.http.HttpServletRequest;

import jodd.mail.Email;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.UUIDGenerator;
import com.sirdc.modules.utils.ValidUtils;

/**
 * 用户api
 * @author: huangcheng.dong
 * @version Revision: 0.0.1
 * @Date: 2015年2月1日
 */
@Controller
@RequestMapping("/api/sys/user/")
public class SysUserApiController extends JsonBaseController {

	@Autowired
	private SysUserService userService;
	@Autowired
	private SysLoginService loginService;
	
	@Override
	protected String getView(String view) {
		return "/modules/api/sys/user/" + view;
	}
	
	
	/**
	 * 校验用户编号是否存在
	 * @author: weihuang.peng
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/isExistForSno")
	public boolean isExistForSno(String sno) {
		return loginService.isExistForSno(sno);
	}
	

	/**
	 * 校验邮箱是否存在
	 * @author: weihuang.peng
	 * @param email
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/isExistForEmail")
	public boolean isExistForEmail(String email) {
		return loginService.isExistForEmail(email);
	}
	
	/**
	 * 校验电话号码是否存在
	 * @author: weihuang.peng
	 * @param mobile
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/isExistForMobile")
	public boolean isExistForMobile(String mobile) {
		return loginService.isExistForMobile(mobile);
	}
	
	/**
	 * 找回密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/searchPassword")
	public Message searchPassword(String email, HttpServletRequest request) {
		if(!ValidUtils.valid(email, ValidUtils.EMAIL)) {
			return coverMessage("500", "邮箱格式有误，请重新输入");
		}
		
		if(!loginService.isExistForEmail(email)) {
			return coverMessage("500", "此邮箱尚未注册,<a href='javascript:location.reload();'>现在注册?</a>");
		}
		
		String uuid = UUIDGenerator.uuid2();
		StringBuffer changePath = new StringBuffer(String.format("%s://%s:%s", request.getScheme(), request.getServerName(), request.getServerPort())).append(request.getContextPath()).append("/api/modules/sys/user/changePasswordWithSearch");
		changePath.append("?email=").append(email);
		changePath.append("&key=").append(uuid);
		Email mail = Email.create();
		mail.addHtml("<a href='" + changePath + "'>点击此处进行更换密码</a>");
        mail.addText("邮件内容");//文本消息
        mail.from("xmutsoft@163.com").to(email);
        mail.subject("来自创新研发中心的密码重置邮件");//标题
        SmtpServer<?> smtpServer = SmtpServer.create("smtp.163.com").authenticateWith("xmutsoft@163.com", "soft333999");
        SendMailSession mailSession = smtpServer.createSession();
        mailSession.open();
        mailSession.sendMail(mail);
        mailSession.close();
        
        SysLogin login = loginService.getUserByLoginName(email);
        return coverMessage("200");
	}
	
	/**
	 * 找回密码的更新操作
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/changePasswordWithSearch")
	public Message changePasswordWithSearch(String email, String key) {
		if(!ValidUtils.valid(email, ValidUtils.EMAIL)) {
			return coverMessage("500", "邮箱格式有误，请重新输入");
		}
		if(!loginService.isExistForEmail(email)) {
			return coverMessage("500", "此邮箱尚未注册,<a href='javascript:location.reload();'>现在注册?</a>");
		}
		
		SysLogin login = loginService.getUserByLoginName(email);

		return coverMessage("200");
	}
}
